*****************************************************************************************************************************************************************
* STUDY 1
* Replication code for "It’s not over when it’s over" - Post-decision arrangements and empirical legitimacy, Journal of Public Administration Research and Theory 
* Authors: Jenny de Fine Licht, University of Gothenburg, jenny.definelicht@spa.gu.se - Peter Esaiasson, University of Gothenburg, peter.esaiasson@pol.gu.se - 
* Mattias Agerberg, University of Gothenburg, mattias.agerberg@gu.se
******************************************************************************************************************************************************************

use "... study 1 data.dta", clear 
cd "..."
local path "..."


************************************************************************************************************************************************
****** DATA MANAGEMENT ******
************************************************************************************************************************************************

// Clean up
drop t_q20* t_q308* t_q310* t_q314* t_q329* fl_1* do_* 

foreach j in 1 2 4 {
	foreach i in t_q*_`j' {
	drop `i'
	}
}

// Outcome variables
gen fair_procedure = q38+q32
replace fair_procedure = (fair_procedure-2)/8
label var fair_procedure "Fairness of procedure"

gen decision_accept = q34+q36
replace decision_accept = (decision_accept-2)/8
label var decision_accept "Decision acceptance"

gen decisionmaker_trust = q40+q42+q44
replace decisionmaker_trust = (decisionmaker_trust-3)/12
label var decisionmaker_trust "Trust in decision makers"

label var q21 "Reasonable decision?"

label var q46 "Accept decision?"

gen outcome_combined = q38+q32+q34+q36+q40+q42+q44
replace outcome_combined = (outcome_combined-7)/28
label var outcome_combined "Outcomes combined"
alpha q38 q32 q34 q36 q40 q42 q44, item

gen outcome_study2 = q38+q34+q40
replace outcome_study2 = (outcome_study2-3)/12
label var outcome_study2 "Outcomes combined, 3 items"

gen outcome_binary = .
replace outcome_binary = 1 if outcome_combined >= 0.5
replace outcome_binary = 0 if outcome_combined < 0.5
label var outcome_binary "Overall legitimate decision"

// Treatments
* Affected
recode s2gr1 (2=0)
label define s2gr1_lab 1 "Directly affected" 0 "Not directly affected", add 
label values s2gr1 s2gr1_lab
label var s2gr1 "Affected"

* Pre-investigation
recode s2gr2 (2=0)
label define s2gr2_lab 1 "In-house administrators" 0 "Independent experts", add 
label values s2gr2 s2gr2_lab
label var s2gr2 "Pre-investigation"

* Pre-decision dialogue
recode s2gr3 (2=0) 
label define s2gr3_lab 0 "Invitation to dialogue" 1 "No invitation", add 
label values s2gr3 s2gr3_lab
label var s2gr3 "Pre-decision dialogue"

* Announcement
recode s2gr4 (2=1) (1=0)
label define s2gr4_lab 1 "Direct" 0 "Indirect", add 
label values s2gr4 s2gr4_lab
label var s2gr4 "Announcement"

* Explanation
replace s2gr5 = s2gr5*(-1)+5
label define s2gr5_lab 1 "No explanation" 2 "Premises and equipment" 3 "Qualified staff" 4 "Budgetary constraints", add 
label values s2gr5 s2gr5_lab
label var s2gr5 "Justification"

* Democratic process
recode s2gr6 (2=0) 
label define s2gr6_lab 1 "Ref. democratic process" 0 "No reference", add 
label values s2gr6 s2gr6_lab
label var s2gr6 "Principled explanation (a)"

* Equal opportunities
recode s2gr7 (2=0) 
label define s2gr7_lab 1 "Ref. equal opportunities" 0 "No reference", add 
label values s2gr7 s2gr7_lab
label var s2gr7 "Principled explanation (b)"

* Post-decision dialogue
recode s2gr8 (2=0) 
label define s2gr8_lab 1 "Invite to post-dec. dialogue" 0 "No invitation", add 
label values s2gr8 s2gr8_lab
label var s2gr8 "Post-decision dialogue"

* Good losers prime
recode s2gr9 (2=1) (1=0)
label define s2gr9_lab 1 "Prime" 0 "No prime", add 
label values s2gr9 s2gr9_lab
label var s2gr9 "Good losers prime"

// Covariates
gen high_pol_interest = .
replace high_pol_interest = 0 if q307 <= 3
replace high_pol_interest = 1 if q307 == 4
label var high_pol_interest "High political interest"

label define sex_lab 1 "Female" 2 "Male", add 
label values sex sex_lab
label var sex "Gender"
gen female = sex
recode female (2=0)

label var q313 "Left-right" 
replace q313 = q313/10

gen university_ed = 0
replace university_ed = 1 if q328 >= 7
replace university_ed = . if q328 == .
label var university_ed "University education"

gen high_pol_trust = .
replace high_pol_trust = 0 if inrange(q309,2,4)
replace high_pol_trust = 1 if q309 == 1
label var high_pol_trust "High political trust"

gen under_40 = .
replace under_40 = 0 if inrange(age6,3,6)
replace under_40 = 1 if inrange(age6,1,2)
label var under_40 "Under 40"

gen under_50 = .
replace under_50 = 0 if inrange(age6,4,6)
replace under_50 = 1 if inrange(age6,1,3)
label var under_50 "Under 50"

xtile pct_screentime = t_q22_3, nq(100)
label var pct_screentime "Screentime for info: percentiles"

// Descriptives (Appendix)
tabstat under_40 university_ed female high_pol_interest high_pol_trust q313, s(mean sd min max n) format(%10.3fc) c(statistics)


************************************************************************************************************************************************
****** MAIN MANUSCRIPT ******
************************************************************************************************************************************************

// Graphs
***(Figure 1)*** 
* Histogram: Initial reaction
hist q21, scheme(s2mono) xlabel(1 `" "Completely" "unreasonable" "' 2 `" "Quite" "unreasonable" "' 3 `" "Neither reasonable" "nor unreasonable" "' 4 `" "Quite" "reasonable" "' 5 `" "Completely" "reasonable" "') /// 
frac ytitle("Density", margin(medsmall)) title("Spontaneous reactions to decision", margin(medsmall)) xtitle(" ") graphregion(margin(l+0 r+4 t+0 b+0)) 
* graph export  "`path'initial_reaction.pdf", replace

***(Figure 2)*** 
* Main treatments graph, combined outcomes
reg outcome_combined i.s2gr4 i.s2gr5 i.s2gr6 i.s2gr7 i.s2gr8 i.s2gr3 i.s2gr2, robust

coefplot, base drop(_cons) xline(0, lp(dash) lc(gs8)) scheme(s2mono) mc(black) /// 
headings(1.s2gr5 = "{bf:Substantial explanation}" /// 
 0.s2gr7 = "{bf:Principled explanation (b)}" 0.s2gr8 = "{bf:Post-decision voice}" 0.s2gr6 = "{bf:Principled explanation (a)}" /// 
 0.s2gr2 = "{bf:Decision guidance docs}" 0.s2gr3 = "{bf:Pre-decision voice}" 0.s2gr4 = "{bf:Announcement}" ) ///
 groups(0.s2gr4 1.s2gr8 = "{bf: Post-decision}" 0.s2gr3 1.s2gr2 = "{bf: Pre-decision}", angle(rvertical))  yscale(alt axis(2)) ///
xtitle(Coefficient, margin(medsmall)) title(Empirical legitimacy, margin(medsmall)) /// 
xscale(r(-0.05(0.05)0.1)) xlabel(-0.05 "-.05" 0 "0" 0.05 ".05" 0.1 ".1") ysize(6.5) graphregion(margin(l+10 r+6 t+0 b+0)) scale(0.8)
* graph export  "`path'predicted_legitimacy_combined.pdf", replace

***(Figure 3)*** 
* Predicted level of perceived legitimacy - different justifications (binary)
reg outcome_binary i.s2gr1 i.s2gr2 i.s2gr3 i.s2gr4 i.s2gr5 i.s2gr6 i.s2gr7 i.s2gr8, robust

margins, at(s2gr5==(4) s2gr6==(0) s2gr7==(0) s2gr8==(1)) at(s2gr5==(2) s2gr6==(1) s2gr7==(1) s2gr8==(0))
marginsplot, recast(scatter) scheme(s2mono) plotregion(margin(l+15 r+15)) plot1opts(mcolor(black) ms(circle)) graphregion(margin(l+5 r+5)) ci1op(lc(black)) /// 
xlabel(1 `" "Worst" "procedure" "' 2 `" "Best" "procedure" "') xtitle(" ") ytitle("Expected share", margin(medsmall)) title("Empirical legitimacy: midpoint or above", margin(medsmall)) xsize(4) ysize(4) ///
yscale(r(0 1)) ylabel(0 "0" 0.1 " " 0.2 ".2" 0.3 " " 0.4 ".4" 0.5 " " 0.6 ".6" 0.7 " " 0.8 ".8" 0.9 " " 1 "1") yline(0.5, lcolor(gs12))
* graph export  "`path'probability_legitimate.pdf", replace


************************************************************************************************************************************************
****** APPENDIX ******
************************************************************************************************************************************************

* Appendix B: Moderating effect of personal involvement and pre-decision arrangements 
* Personal involvement
reg outcome_combined i.s2gr1 i.s2gr2 i.s2gr3 i.s2gr4 i.s2gr5 i.s2gr6 i.s2gr7 i.s2gr8
est store a

reg outcome_combined i.s2gr1##i.s2gr2 i.s2gr1##i.s2gr3 i.s2gr1##i.s2gr4 i.s2gr1##i.s2gr5 i.s2gr1##i.s2gr6 i.s2gr1##i.s2gr7 i.s2gr1##i.s2gr8
est store b

ftest a b

* Pre-decision interacted w post-decision
* Pre-investigation
reg outcome_combined i.s2gr2 i.s2gr4 i.s2gr5 i.s2gr6 i.s2gr7 i.s2gr8
est store a

reg outcome_combined i.s2gr2 i.s2gr2##i.s2gr4 i.s2gr2##i.s2gr5 i.s2gr2##i.s2gr6 i.s2gr2##i.s2gr7 i.s2gr2##i.s2gr8
est store b

ftest a b

* Dialogue
reg outcome_combined i.s2gr3 i.s2gr4 i.s2gr5 i.s2gr6 i.s2gr7 i.s2gr8
est store a

reg outcome_combined i.s2gr3 i.s2gr3##i.s2gr4 i.s2gr3##i.s2gr5 i.s2gr3##i.s2gr6 i.s2gr3##i.s2gr7 i.s2gr3##i.s2gr8
est store b

ftest a b

* Appendix B: Predicting empirical legitimacy with spontaneous reaction 
* Predicted level of empirical legitimacy - different spontaneous reactions
reg outcome_combined i.q21, robust

margins, at(q21==(1(1)5))
marginsplot, recast(scatter) scheme(s2mono) graphregion(margin(l+3 r+3)) plot1opts(mcolor(black) ms(circle))  ci1op(lc(black)) /// 
xlabel(1 `" "Completely" "unreasonable" "' 2 " " 3 "Neutral" 4 " " 5 `" "Completely" "reasonable" "') /// 
xtitle("Decision: spontaneous reaction", margin(medsmall)) ytitle("Predicted empirical legitimacy", margin(medsmall)) title("Empirical legitimacy: spontaneous reactions", margin(medsmall)) ///
yscale(r(0 1)) ylabel(0 "0" 0.25 ".25" 0.5 ".5" 0.75 ".75" 1 "1")
* graph export  "`path'predicted_legitimacy_initial.pdf", replace

* Appendix B: Main treatment effects with 3-item index 
* Main treatments graph, combined outcomes (3 items)
reg outcome_study2 i.s2gr4 i.s2gr5 i.s2gr6 i.s2gr7 i.s2gr8  i.s2gr3 i.s2gr2, robust

coefplot, base drop(_cons) xline(0, lp(dash) lc(gs8)) scheme(s2mono) mc(black) /// 
headings(1.s2gr5 = "{bf:Substantial explanation}" /// 
 0.s2gr7 = "{bf:Principled explanation (b)}" 0.s2gr8 = "{bf:Post-decision voice}" 0.s2gr6 = "{bf:Principled explanation (a)}" /// 
 0.s2gr2 = "{bf:Decision guidance docs}" 0.s2gr3 = "{bf:Pre-decision voice}" 0.s2gr4 = "{bf:Announcement}" ) ///
 groups(0.s2gr4 1.s2gr8 = "{bf: Post-decision}" 0.s2gr3 1.s2gr2 = "{bf: Pre-decision}", angle(rvertical))  yscale(alt axis(2)) ///
xtitle(Coefficient, margin(medsmall)) title(Empirical legitimacy, margin(medsmall)) /// 
xscale(r(-0.05(0.05)0.1)) xlabel(-0.05 "-.05" 0 "0" 0.05 ".05" 0.1 ".1") ysize(6.5) graphregion(margin(l+10 r+6 t+0 b+0)) scale(0.8)
* graph export  "`path'predicted_legitimacy_combined3items.pdf", replace


